﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.ComponentModel;
using XCI.Sys.Service;
using XCI.Core;
using XCI.Windows.Controls;
using XCI.Sys.Model;
using XCI.Windows;
using System.Collections.Generic;

namespace XCI.Sys.Controls
{
    /// <summary>
    /// 用户下拉控件
    /// </summary>
    public class UserComboBoxEdit : ComboBoxEditx
    {
        private readonly AsyncWorker asyncWorker = new AsyncWorker();
        public override string ValueMember => "Id";
        public override string DisplayMember => "Name";

        /// <summary>
        /// 是否显示全部项
        /// </summary>
        [DefaultValue(true)]
        [Category("XCI")]
        [Description("是否显示全部项")]
        public virtual bool AllItemVisible { get; set; } = true;

        /// <summary>
        /// 全部项文本
        /// </summary>
        [DefaultValue("全部分类")]
        [Category("XCI")]
        [Description("全部项文本")]
        public virtual string AllItemCaption { get; set; } = "全部";

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (DesignMode) return;
            List<SysUserSimple> list = null;
            asyncWorker
                .RunCallback(e => { list = UserService.Instance.SelectSimpleList(null, null, true); })
                .CompletCallback(e => 
                {
                    if (AllItemVisible)
                    {
                        list.Insert(0, new SysUserSimple
                        {
                            Id = string.Empty,
                            Name = AllItemCaption
                        });
                    }
                    DataSource = list;
                })
                .RunAsync();
        }
    }
}